package com.ihd.ihardware.view.lock.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemZwAuthBinding;
import com.ihd.ihardware.databinding.ItemZwBinding;
import com.ihd.ihardware.pojo.Test_zw;
import com.ihd.ihardware.pojo.Test_zw_auth;
import com.ihd.ihardware.view.lock.view.LockZWAuthActivity;
import com.ihd.ihardware.view.tzc.health.model.ReportRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class LockZWInputViewModel extends AndroidViewModel {

    private ReportRepository mReportRepository;

    public LockZWInputViewModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<Test_zw> mMembers = new ObservableArrayList<>();
    public CommonAdapter mZWAdapter = new CommonAdapter<Test_zw, ItemZwBinding>(R.layout.item_zw, mMembers, null) {
        @Override
        public void convert(ItemZwBinding mDataBinding, Test_zw test_concern, int position) {
            mDataBinding.head.setImageResource(test_concern.getIcon());
            mDataBinding.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            mDataBinding.itemLL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    };

    public void setMembers(List<Test_zw> list) {
        mMembers.clear();
        if (list != null && list.size() > 0)
            mMembers.addAll(list);
    }

    public void delMember(String memberId) {
    }

    private MDialog dg;
    private ObservableArrayList<Test_zw_auth> mZWAuth = new ObservableArrayList<>();
    public CommonAdapter mZWAuthAdapter = new CommonAdapter<Test_zw_auth, ItemZwAuthBinding>(R.layout.item_zw_auth, mZWAuth, null) {
        @Override
        public void convert(ItemZwAuthBinding mDataBinding, Test_zw_auth tza, int position) {
            if (tza.getStatus() == 0) {
                mDataBinding.head.setImageResource(R.drawable.lock_listicon_timeing);
                mDataBinding.status.setText("查看");
                mDataBinding.status.setTextColor(Color.parseColor("#936AE5"));
                mDataBinding.status.setBackgroundResource(R.drawable.corners_line_936ae5);
                mDataBinding.status.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dg = DialogUtils.alertDialog((LockZWAuthActivity) view.getContext(), MDialog.DG_TYPE.ZWALERT, tza.getTime() + "向你申请开锁，请确认申请人身份是否同意授权开锁", "不同意", "同意授权", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dg.cancel();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dg.cancel();
                                DialogUtils.successDialog((LockZWAuthActivity) view.getContext(), MDialog.DG_TYPE.ZWSUCCESS, 0, "");
                            }
                        });
                    }
                });
            } else {
                mDataBinding.head.setImageResource(R.drawable.lock_listicon_time);
                if (tza.getStatus() == 1)
                    mDataBinding.status.setText("同意");
                else
                    mDataBinding.status.setText("不同意");
                mDataBinding.status.setTextColor(Color.parseColor("#99FFFFFF"));
                mDataBinding.status.setBackgroundResource(0);
                mDataBinding.status.setOnClickListener(null);
            }
        }
    };

    public void setZWAuths(List<Test_zw_auth> list) {
        mZWAuth.clear();
        if (list != null && list.size() > 0)
            mZWAuth.addAll(list);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mReportRepository) {
            mReportRepository.onDestroy();
            mReportRepository = null;
        }
    }
}
